//
//  IwantWithdrawViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/7.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "IwantWithdrawViewController.h"
#import "AddBankViewController.h"


@interface IwantWithdrawViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    
    UITableView * _tableView;
    NSArray * _dataArray;
    UITextField * moneyText;
    NSDictionary * chooseDic;
    NSString * moneyStr;

}
@property (assign, nonatomic) NSIndexPath *selIndex;

@end

@implementation IwantWithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"我要提现"];
    [self creatTableView];
    [self getListData];
    moneyStr = @"";
    // 支付密码输入框
    self.verifyCodeInputView = [[VerifyCodeInputView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight)];
    self.verifyCodeInputView.inputView.delegate = self;
    [self.verifyCodeInputView setHidden:YES];
    [self.view addSubview:self.verifyCodeInputView];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightItemWithTitle:@"加银行卡" target:self action:@selector(cddCarClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)cddCarClick{
    
    AddBankViewController * add = [[AddBankViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_NavBarHeight) style:UITableViewStyleGrouped];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 160;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 200;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString * cellId = @"cellId";
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:cellId ];
    if (cell==nil) {
        
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }else {
        while ([cell.contentView.subviews lastObject] != nil) {
            
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    NSDictionary * dic = _dataArray[indexPath.row];
    UILabel * titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, WIDTH -100, 20)];
    titleLabel.text = dic[@"bank_name"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:titleLabel];
    
    UILabel * detailsLabel  = [[UILabel alloc]initWithFrame:CGRectMake(15, 60, WIDTH -60, 20)];
    detailsLabel.text = [NSString stringWithFormat:@"%@(%@)",dic[@"bank_username"],dic[@"bank_num"]];
    detailsLabel.textColor = [UIColor lightGrayColor];
    detailsLabel.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:detailsLabel];
    
    
    
     UIImageView * chooseImage = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 35, 47.5, 25, 25)];
    if (_selIndex == indexPath) {
       
        chooseImage.image = ImageName(@"icon_shoppingcat_choice");
       
    }else {
        
      
        chooseImage.image = ImageName(@"icon_shoppingcat_choice_pre");
      
    }
     [cell.contentView addSubview:chooseImage];
   
    
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 160)];
    heardView.backgroundColor = [UIColor whiteColor];
    
    UILabel * titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, WIDTH -20, 20)];
    titleLabel.text = @"提现金额";
    titleLabel.font = [UIFont systemFontOfSize:15];
    [heardView addSubview:titleLabel];
    
    UILabel * iconLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 50,30, 30)];
    iconLabel.text = @"￥";
    iconLabel.font = [UIFont systemFontOfSize:20];
    [heardView addSubview:iconLabel];
    
    moneyText = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, WIDTH - 50, 30)];
     moneyText.placeholder = @"请输入提现金额";
    if (moneyStr.length>0) {
        moneyText.text = moneyStr;
    }else{
        
    }
    
    moneyText.borderStyle =  UITextBorderStyleNone;
    moneyText.font = [UIFont systemFontOfSize:18];
    moneyText.delegate = self;
    [heardView addSubview:moneyText];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(40, 90, WIDTH - 50, 0.5)];
    line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [heardView addSubview:line];
    
    UILabel * balanceLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 110, WIDTH -20, 20)];
    balanceLabel.text = [NSString stringWithFormat:@"账户余额%@元 ",self.money];
    balanceLabel.font = [UIFont systemFontOfSize:15];
    balanceLabel.textAlignment = NSTextAlignmentRight;
    balanceLabel.textColor = [UIColor lightGrayColor];
    [heardView addSubview:balanceLabel];
    
    
    UIView * line_two = [[UIView alloc]initWithFrame:CGRectMake(0, 150, WIDTH, 10)];
    line_two.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [heardView addSubview:line_two];
    
    
    
    return heardView;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    moneyStr = moneyText.text;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView * footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    footerView.backgroundColor  = [UIColor groupTableViewBackgroundColor];
    UILabel * titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, WIDTH -20, 20)];
    titleLabel.text = @"注：尊敬的用户，提现后您的钱包余额不能少于10元，每次提现不能超过5万元，请悉知";
    titleLabel.textColor =Color(75, 105, 161);
    titleLabel.numberOfLines = 2;
    [titleLabel sizeToFit];
    titleLabel.font = [UIFont systemFontOfSize:14];
    [footerView addSubview:titleLabel];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake((WIDTH -300)/2, 80, 300, 80);
    [btn setBackgroundImage:ImageName(@"icon_login__buttton_bg") forState:UIControlStateNormal];
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(sureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:btn];
    
    
    return footerView;
}

-(void)sureBtnClick:(UIButton *)btn {
    
    if (moneyText.text.length<=0) {
        SHuiMBhudTextTimeStr(@"请输入提现金额");
        return;
    }else  if ([moneyText.text intValue]%100!=0) {
        
        SHuiMBhudTextTimeStr(@"提现金额必须是100倍数！");
        
        return;
    }else if (chooseDic==nil){
        
        SHuiMBhudTextTimeStr(@"请选择银行卡");
    }else{
        
        [self checkPayPasswordSetting];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    _selIndex = indexPath;
    chooseDic = _dataArray[indexPath.row];
    [_tableView reloadData];
    
}

-(void)getListData{
    
    NSDictionary * dic = @{
                           @"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_bankList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            
            if ([responseObject[@"datas"] isKindOfClass:[NSArray class]]) {
                
                _dataArray = responseObject[@"datas"];
                
            }

        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        [_tableView reloadData ];
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
}

-(void)withDrawRequestFor:(NSString *)password{
    
    NSDictionary * dic = @{
                           @"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"money":moneyStr,
                           @"id":chooseDic[@"id"],
                           @"pay_password":password
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(with_drowMoney_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"提交成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
       
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


- (void)checkPayPasswordSetting {
    

    [self.verifyCodeInputView setHidden:NO];
    [self.verifyCodeInputView.inputView.textFieldArray[0] becomeFirstResponder];
}

#pragma mark - 支付密码输入完成回调
- (void)inputFinishedWithText:(NSString *)text {
    // text为用户输入的6位密码
    
    self.verifyCodeInputView.hidden = YES;
    for(UITextField *textField in self.verifyCodeInputView.inputView.textFieldArray) {
        textField.text = @"";
    }
    
    if (text.length!=6) {
        [SHJudgeTools MBProgressHUDPromptWith:@"支付密码格式不正确" On:self.view];
        return;
    }else {
        
        [self withDrawRequestFor:text];
    }
    
}



@end
